<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/allApp.css" />
		<link rel="stylesheet" href="css/css.css" />
		<style type="text/css">
			.title {
				margin: 0px;
			}
			.fs15{
				font-size: 15px;
			}
			.fs13{
				font-size: 13px!important;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">现场巡检</h1>
		</header>
		<div class="mui-content">
			<div class="xj-content" id="xj-content">
				<div class="H-10">

				</div>
				<!--<div class="mui-card">
				<div class="mui-card-header">
						<div class="mui-left title">
							风机机位状况
						</div>
						<div class="mui-right mui-right-title">
							状态：A:<span class="staus FtHs">22</span> B:<span class="staus FtHs">30</span> C:<span class="staus FtHs">30</span>
						</div>
				</div>
				<div class="mui-card-content">
						<p>备注：表面无裂纹、无进水和积水、无塌陷</p>
						<div class="img-up">
							<img src="./img/img.png" alt="" />
						</div>
					</div>
				</div>-->

			</div>
		</div>
		<div class="btn">
			<button type="button" class="mui-btn mui-btn-primary mui-col-sm-12 mui-col-xs-12 " id="submit">提交</button>
			<button style="margin-top:10px;display: none;" type="button" class="mui-btn mui-btn-primary mui-col-sm-12 mui-col-xs-12" id="saverecord">保存草稿</button>
		</div>
		<script src="../../js/mui.min.js"></script>
		<script type="text/javascript" src="../../js/mkeyplus.base.xunjian.js"></script>
		<script src="../../js/jquery-1.7.2.min.js"></script>
		<script src="./js/mui.previewimage.js"></script>
		<script src="./js/mui.zoom.js"></script>
		<script type="text/javascript">
			mui.init();
			mui.previewImage();
			var rate;
			var from;
			var scancode = '';
			mui.plusReady(function() {
				var self = plus.webview.currentWebview();
				var userxjId = self.userxjId;
				from = self.from;
				var list = self.list;
				scancode = self.scancode;
				var name = self.name;
				var smTime = self.smTime;
				var index = self.index;
				var type = self.type;
				var offlinetype = self.offlinetype;
				var opener = self.opener();
				var opene_opener = self.opener().opener();
				rate = self.rate;
				var xsdwindid = self.xsdwindid; //新增 巡视点ID
				console.log('xsdwindid='+xsdwindid);
				console.log('scancode='+scancode);
				var shebeibh = self.shebeibh;
				var isnormal = 0;
				var org_code = localStorage.getItem('org_code'); //获取缓存中的风场编号
				console.log(localStorage.getItem("curentTime"));
				console.log(smTime)
//				var org_code1 = JSON.parse(org_code);

				console.log(JSON.stringify(list));
				console.log(userxjId);
				for(var i = 0; i < list.length; i++) {
					var str = "";
					str += '<div class="mui-card"data-id="' + list[i].id + '">';
					if(list[i].data_type == "0") {
						if(list[i].staus == "异常") {
							str += '<div class="mui-card-header"data-id="' + list[i].data_type + '"><div class="mui-left fs15 title">' + list[i].Title + '</div><div class="mui-right mui-right-title">' + list[i].desc_name + '<span class="FtHs staus">' + list[i].staus + '</span></div></div>';
						} else {
							str += '<div class="mui-card-header"data-id="' + list[i].data_type + '"><div class="mui-left fs15 title">' + list[i].Title + '</div><div class="mui-right mui-right-title">' + list[i].desc_name + '<span class="staus">' + list[i].staus + '</span></div></div>';
						}
					} else if(list[i].data_type == "1") {
						str += '<div class="mui-card-header"data-id="' + list[i].data_type + '"><div class="mui-left fs15 title">' + list[i].Title + '</div><div class="mui-right mui-right-title">' + list[i].desc_name + '<span class="staus">' + list[i].staus + '</span></div></div>';
					} else if(list[i].data_type == "2") {
						str += '<div class="mui-card-header"data-id="' + list[i].data_type + '"><div class="mui-left fs15 title">' + list[i].Title + '</div><div class="mui-right mui-right-title">' + list[i].desc_name + 'A:<span class="staus">' + list[i].staus[0].value + '</span>,B:<span class="staus">' + list[i].staus[1].value + '</span>,C:<span class="staus">' + list[i].staus[2].value + '</span></div></div>';
					}
					str += '<div class="mui-card-content"><p class="fs13">备注：<span class="beizhu ">' + list[i].desc + '</span><span class="hide else">暂无备注</span></p><div class="img-up"><img src="' + list[i].img_src + '" alt="" data-preview-src=""data-preview-group="' + (i + 1) + '"/></div></div>';
						str += '</div>';

					
					$(".xj-content").append(str);
				};

				var arr = [];
				for(var i = 0; i < list.length; i++) {
					var obj = {};
					obj.desc = list[i].desc;
					obj.id = list[i].id;
					
					if(list[i].data_type == "2"){
						obj.state = "A:"+list[i].staus[0].value+","+"B:"+list[i].staus[1].value+","+"C:"+list[i].staus[2].value;
						/*state1.A = list[i].staus[0].value;
						state1.B = list[i].staus[1].value;
						state1.C = list[i].staus[2].value;*/
//						obj.state =eval("("+state1+")");
					}else{
						obj.state = list[i].staus;
					}
					obj.attach = list[i].attach;
					arr.push(obj);
				}
				var jcxArray = JSON.stringify(arr);
				for(var i=0;i<arr.length;i++){
					if(arr[i].desc != ""){
						isnormal = 1;
					}
				}
				console.log('有网状态下提交数据'+JSON.stringify(arr));
				console.log(jcxArray);

				//获取无网状态下保存的项目
				var list = $('#xj-content .mui-card');
				var wwarr = [];
				for(var i = 0; i < list.length; i++) {
					var obj = {};
					obj.Title = $(list[i]).find('.title').text();
					obj.desc = $(list[i]).find('.beizhu').text();
					obj.id = $(list[i]).attr('data-id');
					if($(list[i]).find(".staus").length==1){
						obj.state = $(list[i]).find(".staus").text();
					}else{
						obj.state="A:"+$(list[i]).find(".staus").eq(0).text()+","+"B:"+$(list[i]).find(".staus").eq(1).text()+","+"C:"+$(list[i]).find(".staus").eq(2).text();
					}
					
					obj.attach = $(list[i]).find('img').attr('src');
					wwarr.push(obj);
				}
				var xjxArray = [];
				var obj1 = {};
				obj1.name = name;
				obj1.smTime = smTime;
				obj1.scancode = scancode;
				console.log(JSON.stringify(xjxArray));
				console.log('wwarr'+JSON.stringify(wwarr));

				/**
				 * 生成一个用不重复的ID
				 */
				function GenNonDuplicateID() {
					var idStr = Date.now().toString(36)
					idStr += Math.random().toString(36).substr(3)
					return idStr
				}
				//函数将生成类似 ix49sfsnt7514k5wpflyb5l2vtok9y66r 的ID
				GenNonDuplicateID()

				//				无网状态下提交
				var savexcxj = JSON.parse(localStorage.getItem('savexcxj')),
					dataArr = [];
				//如果localStorage.getItem('savexcxj')存在值，就先添加进数组里面
				if(savexcxj) {
					for(var i = 0; i < savexcxj.length; i++) {
						dataArr.push(savexcxj[i]);
					}
				}
				
				/* 主动保存草稿箱 */
				var saverecord = function(){
					//localStorage.removeItem("savexcxj")
					var data = {
						xjxArray: obj1,
						jcxArray: wwarr,
						userxjId: GenNonDuplicateID(),
						org_code:org_code,
						index: index,
						xsdwindid:xsdwindid
					}
					dataArr.push(data);
					mui.toast('当前无网络已保存到本地！')
					// mui.openWindow({
					// 	url: 'jiankong.html',
					// 	id: 'jiankong.html',
					// 	extras: {}
					// });
					plus.webview.currentWebview().close();
					if(from == 'news'){
						var page2 = plus.webview.getWebviewById('xianchangxunjian.html');
						var page1 = plus.webview.getWebviewById('weixunjianlist.html');
						if(page2){
							page2.close('none');
						}
						if(page1){
							if(offlinetype == 'pinci'){
								var pcchild = plus.storage.getItem('pcchild') || '[]';
								pcchild = JSON.parse(pcchild);
								for (var i = 0; i < pcchild.length; i++) {
									if(pcchild[i].key == xsdwindid){
										var arr = pcchild[i].value;
										for (var j = 0; j < arr.length; j++) {
											if(arr[j].number == scancode){
												arr[j].flags = true;
												pcchild[i].value = arr;
											}
										}
									}
								}
								plus.storage.setItem('pcchild',JSON.stringify(pcchild));
							}else if(offlinetype == 'xianlu'){
								var xlchild = plus.storage.getItem('xlchild') || '[]';
								xlchild = JSON.parse(xlchild);
								for (var i = 0; i < xlchild.length; i++) {
									if(xlchild[i].key == xsdwindid){
										var arr = xlchild[i].value;
										for (var j = 0; j < arr.length; j++) {
											if(arr[j].number == scancode){
												arr[j].flags = true;
												xlchild[i].value = arr;
											}
										}
									}
								}
								plus.storage.setItem('xlchild',JSON.stringify(xlchild));
							}
							mui.fire(page1, 'refresh');
						}
					}else{
						var page1 = plus.webview.getWebviewById('pinlv.html');
						var page2 = plus.webview.getWebviewById('../../xianchangxunjian.html');
						var page3 = plus.webview.getWebviewById('new/offline/dxjlist.html');
						if(page2){
							page2.close('none');
						}
						if(page1){
							page1.close('none');
						}
						
						var sb = plus.storage.getItem('sb') || '[]';
						sb = JSON.parse(sb);
						for (var i = 0; i < sb.length; i++) {
							if(sb[i].number == scancode && sb[i].rate == index){
								sb[i].flags = true;
							}
						}
						plus.storage.setItem('sb',JSON.stringify(sb));
						mui.fire(page3, 'refresh');
						
					}
					//页面关闭时把contrastdataArr存储进localStorage(dataArr)
					localStorage.setItem('savexcxj', JSON.stringify(dataArr));
					console.log("****。。" + JSON.stringify(dataArr));
					console.log("****。。" + JSON.stringify(savexcxj));
					return ;
				}
				document.querySelector('#saverecord').addEventListener('tap', function(){
					saverecord()
				});
				
				document.querySelector('#submit').addEventListener('tap', function() {
					if (plus.networkinfo.getCurrentType()  ==  plus.networkinfo.CONNECTION_NONE)  {
						var offline = plus.storage.getItem('offline');
						if(offline && offline == '1'){
						}else{
							mui.toast('现已离线，请在草稿箱操作');
						}
						setTimeout(()=>{
							saverecord()
						},1000)
					} else {
						var btnArr = ['确认', '取消'];
						mui.confirm('确认提交吗?', '操作提示', btnArr, function(e) {
							if(e.index == 0) {
								console.log('111111');
								plus.nativeUI.showWaiting('提交中...');
								mui.baseApp.httpRequst('/api/v2/sbxj/submitUserXj', {
									jcxArray: jcxArray,
									memberId: localStorage.getItem('userid'),
									userxjId: userxjId,
									scancode: scancode,
									rate: index,
									isnormal:isnormal,
									inspect_time: localStorage.getItem("curentTime")
								}, function(data) {
									setTimeout(()=>{
										plus.nativeUI.closeWaiting();
									},800)
									console.log('111111' + JSON.stringify(data));
									if(data.status == "200") {
										mui.toast(data.message);
										console.log('from........',from);
										//为了不影响旧功能，传参数单独处理新功能的返回
										if(from == 'news'){
											setTimeout(()=>{
												var page1 = plus.webview.getWebviewById('weixunjianlist.html');
												mui.fire(page1, 'refresh');
												var page = plus.webview.getWebviewById("jiankong.html");
												mui.fire(page, 'home');
												if(offlinetype == 'pinci'){
													updatePcChild(xsdwindid);
												}else if(offlinetype == 'xianlu'){
													updateXlChild(xsdwindid);
												}
												opener.close('none');
												self.close('none');
											},1000)
											// if(type && type=='scan'){
											// 	console.log('nani....');
											// 	opene_opener.close('none');
											// }
										}else if(from == 'shebei'){
											setTimeout(()=>{
												var page1 = plus.webview.getWebviewById('dxjchild/sb_sub.html');
												mui.fire(page1, 'refresh');
												//在线提交，也需要判断草稿箱是否有数据，有的话删除
												deleteSbList(scancode,index);
												draglistremove(scancode,index);
												
												var page = plus.webview.getWebviewById("jiankong.html");
												mui.fire(page, 'home');
												opene_opener.close('none');
												opener.close('none');
												self.close('none');
											},1000)
											
										}else{
											mui.openWindow({
												url: 'jiankong.html',
												id: 'jiankong.html',
												extras: {
													jcxArray: jcxArray,
													userxjId: userxjId
												}
											});
											plus.webview.currentWebview().close();
											opener.close('none');
											opene_opener.close('none');
											var page = plus.webview.getWebviewById("jiankong.html");
											mui.fire(page, 'home');
											plus.webview.currentWebview().opener().close();
										}
									} else if(data.status == "400") {
										mui.toast(data.message);
									} else if(data.status == "409") {
										mui.toast(data.message);
									}

								},function(){},'POST')
							}

						});
					}

				});

				var contentlist = $('.mui-card-content');
				for(var i = 0; i < contentlist.length; i++) {
					var img_src = $(contentlist[i]).find('img').attr('src');
					var beizhu = $(contentlist[i]).find('.beizhu').text();
					if(img_src == "") {
						$(contentlist[i]).find('.img-up').addClass('hide');
					}
					if(beizhu == "") {
						$(contentlist[i]).find('.beizhu').addClass('hide');
						$(contentlist[i]).find('.else').removeClass('hide');
					}
				}
			})
			
			function updatePcChild(xsdwindid){
				var pcchild = plus.storage.getItem('pcchild') || '[]';
				pcchild = JSON.parse(pcchild);
				for (var i = 0; i < pcchild.length; i++) {
					if(pcchild[i].key == xsdwindid){
						var arr = pcchild[i].value;
						for (var j = 0; j < arr.length; j++) {
							if(arr[j].number == scancode){
								arr[j].status = 1;
								pcchild[i].value = arr;
							}
						}
					}
				}
				plus.storage.setItem('pcchild',JSON.stringify(pcchild));
			}
			
			function updateXlChild(xsdwindid){
				var xlchild = plus.storage.getItem('xlchild') || '[]';
				xlchild = JSON.parse(xlchild);
				for (var i = 0; i < xlchild.length; i++) {
					if(xlchild[i].key == xsdwindid){
						var arr = xlchild[i].value;
						for (var j = 0; j < arr.length; j++) {
							if(arr[j].number == scancode){
								arr[j].status = 1;
								xlchild[i].value = arr;
							}
						}
					}
				}
				plus.storage.setItem('xlchild',JSON.stringify(xlchild));
			}
			
			//提交成功后判断草稿箱中是否有，有的话删除
			function deleteSbList(scancode,index) {
				var sb = plus.storage.getItem('sb');
				console.log('设备.....'+JSON.stringify(sb));
				sb = JSON.parse(sb);
				for(var m = 0; m < sb.length; m++) {
					if(sb[m].number == scancode && sb[m].rate == index) {
						sb.splice(m, 1);
						plus.storage.setItem('sb', JSON.stringify(sb));
					}
				}
			}
			
			//提交成功后判断草稿箱中是否有，有的话删除
			function draglistremove(scancode,index) {
				var save = JSON.parse(localStorage.getItem("savexcxj"));
				if(save){
					for(var m = 0; m < save.length; m++) {
						if(save[m].xjxArray.scancode == scancode && save[m].index == index) {
							save.splice(m, 1);
							console.log("提交后的草稿箱数组1068。。" + JSON.stringify(save))
							localStorage.setItem("savexcxj", JSON.stringify(save));
						}
					}
				}
			}
		</script>
	</body>

</html>